package per.turtle.file.utils;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import per.turtle.common.core.exception.file.FileSizeLimitExceededException;
import per.turtle.common.core.exception.file.InvalidExtensionException;
import per.turtle.common.core.utils.file.MimeTypeUtils;
import per.turtle.file.config.FileConfig;

import java.io.IOException;
import java.util.Objects;

/**
 * 文件上传工具类
 *
 * @author turtle
 */
@Component
public class FileUploadUtils {

    /**
     * M 级位运算
     */
    private final static int BIT_OPER_M = 20;

    /**
     * 文件限制
     */
    private static FileConfig fileConfig;

    /**
     * 文件校验
     *
     * @param file 上传的文件
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws InvalidExtensionException      文件格式校验异常
     */
    public static void assertAllowed(MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, InvalidExtensionException {
        // 大小校验
        long size = file.getSize();
        if (size > (fileConfig.getMaxSize() << BIT_OPER_M)) {
            throw new FileSizeLimitExceededException(fileConfig.getMaxSize());
        }

        // 格式校验
        String fileName = file.getOriginalFilename();
        String extension = null;
        try {
            extension = getExtension(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
            if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION) {
                throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, fileName);
            } else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION) {
                throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, fileName);
            } else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION) {
                throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, fileName);
            } else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION) {
                throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension, fileName);
            } else {
                throw new InvalidExtensionException(allowedExtension, extension, fileName);
            }
        }
    }

    /**
     * 获取文件名的后缀
     *
     * @param file 表单文件
     * @return 后缀名
     */
    public static String getExtension(MultipartFile file) throws IOException {
        String extension = FileNameUtil.getSuffix(file.getOriginalFilename());
        if (StrUtil.isEmpty(extension)) {
            System.out.println(FileTypeUtil.getType(file.getInputStream()));
            extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
        }
        return extension;
    }

    /**
     * 判断MIME类型是否是允许的MIME类型
     *
     * @param extension        上传文件类型
     * @param allowedExtension 允许上传文件类型
     * @return true/false
     */
    public static boolean isAllowedExtension(String extension, String[] allowedExtension) {
        for (String str : allowedExtension) {
            if (str.equalsIgnoreCase(extension)) {
                return true;
            }
        }
        return false;
    }

    @Autowired
    public void init(FileConfig fileConfig) {
        FileUploadUtils.fileConfig = fileConfig;
    }

}